$(function () {
    $('#address_add').click(function () {
        $('.bm_myaddress h2.title').removeClass('title_uadate').addClass('title_add').html('新增收货地址');
        $('.bm_myaddress .item-submit').removeClass('submit_uadate').addClass('submit_add').html('保存新地址');
    })
    // 提交收货地址
    var isPhone=/^0?(13|14|15|17|18)[0-9]{9}$/;
    var address_id = 0; // 确认用户收货地址id的标识
    $('#address_info').submit(function (e) {
        e.preventDefault();
        var $this = $(this);
        var isPopAddress=false

        $this.find('.jm_error').html('').prev().removeClass('error');
        $this.find('.city-picker-span').removeClass('jm_input_error');

        var eleProCityArea= $this.find('.form-group span.title')
        if(eleProCityArea.html()==''){
            eleProCityArea.parents('.item').children('.jm_error').html("请选择地区");
            return false;
        }

        var eleAddre=$this.find("textarea[name='address']")
        if(!isempty(eleAddre, '请输入详细地址')){
            eleAddre.next('.jm_error').html('请输入详细地址');
            return false;
        };

        var eleReceiver=$this.find("input[name='receiver']")
        if(!isempty(eleReceiver, '请填写收货人')){
            eleReceiver.next('.jm_error').html('请填写收货人');
            return false;
        };
        if(eleReceiver.val().length>20){
            eleReceiver.next('.jm_error').html('收货人姓名不能超过20个字符。');
            return false;
        };

        var elePhone=$this.find("input[name='phone']")
        if(!isempty(elePhone, '请填写手机号码')){
            elePhone.next('.jm_error').html('请填写手机号码');
            return false;
        };

        var mobile = elePhone.val();
        if (!isPhone.test(mobile)) {
            elePhone.next('.jm_error').html('手机号码格式不正确');
            return false;
        }

        // 地址赋值
        $this.find('input[name="province_code"]').val($this.find(".form-group .select-item:eq(0)").data('code'));
        $this.find('input[name="city_code"]').val($this.find(".form-group .select-item:eq(1)").data('code'));
        $this.find('input[name="area_code"]').val($this.find(".form-group .select-item:eq(2)").data('code'));


        if($this.hasClass('pop-address'))
            isPopAddress=true
        // 修改 submit 提交方式 AJAX
        saveAddress(isPopAddress);



        // var data_province = $(".form-group .select-item:eq(0)").html();
        // var data_province_code = $(".form-group .select-item:eq(0)").data('code');
        // var data_city = $(".form-group .select-item:eq(1)").html();
        // var data_city_code = $(".form-group .select-item:eq(1)").data('code');
        // var data_area = $(".form-group .select-item:eq(2)").html();
        // var data_area_code = $(".form-group .select-item:eq(2)").data('code');
        // var address = $("textarea[name='address']").val();
        // var name = $("input[name='fullname2']").val();


        // if($('.bm_myaddress').attr('add_update')=='0'){
        //     //此处为添加收货地址
        //     if(address_id==0){
        //         var set = '<a href="javascript:void(0);" class="default">默认地址</a>';
        //     }else{
        //         var set = '<a href="javascript:void(0);" class="zdefault">设为默认地址</a>';
        //     }
        //     address_id++;
        //     var str = '<tr data-id="'+address_id+'"><td class="col-name">'+name+'</td><td class="col-address"><span class="province" data-code="'+data_province_code+'">'+data_province+'</span>&nbsp;<span class="city" data-code="'+data_city_code+'">'+data_city+'</span>&nbsp;<span class="area" data-code="'+data_area_code+'">'+data_area+'</span>&nbsp;<span class="address">'+address+'</span></td><td class="col-phone">'+mobile+'</td><td><a href="#bm_add" class="zupdate">修改</a><i class="sep">&nbsp;|&nbsp;</i><a href="javascript:void(0);" class="zdelete">删除</a></td><td class="col-note">'+set+'</td></tr>';
        //     $('.address-tbl tbody').append(str);
        //     $('.address-tbl').show();
        // }else{
        //     //此处为修改收货地址
        //     var tr_id = $(this).attr('data-id');
        //     $.each($('.address-tbl tbody tr'),function () {
        //         if($(this).data('id')==tr_id){
        //             $(this).find('.col-name').html(name);
        //             $(this).find('.province').html(data_province);
        //             $(this).find('.city').html(data_city);
        //             $(this).find('.area').html(data_area);
        //             $(this).find('.address').html(address);
        //             $(this).find('.col-phone').html(mobile);
        //         }
        //     })
        // }

    })
    // 点击修改地址按钮
    $('.address-tbl').delegate('.zupdate','click',function () {
        var data_province = $(this).parent().prevAll('.col-address').children('.province').text();
        var data_province_code = $(this).parent().prevAll('.col-address').children('.province').data('code');
        var data_city = $(this).parent().prevAll('.col-address').children('.city').text();
        var data_city_code = $(this).parent().prevAll('.col-address').children('.city').data('code');
        var data_area = $(this).parent().prevAll('.col-address').children('.area').text();
        var data_area_code = $(this).parent().prevAll('.col-address').children('.area').data('code');
        var address = $(this).parent().prevAll('.col-address').children('.address').html();
        var name = $(this).parent().prevAll('.col-name').html();
        var mobile = $(this).parent().prevAll('.col-phone').html();
        var address_id = $(this).parents('tr').data('id');
        $(".form-group .select-item:eq(0)").html(data_province).attr('data-code',data_province_code);
        $(".form-group .select-item:eq(1)").html(data_city).attr('data-code',data_city_code);
        $(".form-group .select-item:eq(2)").html(data_area).attr('data-code',data_area_code);
        $('.m_myaddress .item-textarea').val(address);
        $('.m_myaddress .item-input-fullname').val(name);
        $('.m_myaddress .item-input-mobile').val(mobile);
        $('.bm_myaddress h2.title').removeClass('title_add').addClass('title_uadate').html('修改收货地址');
        $('.bm_myaddress .item-submit').removeClass('submit_add').addClass('submit_uadate').html('保存地址');
        $('#address_info').attr('data-id',address_id);
        $('.bm_myaddress').attr('add_update','1');
    })
    // 删除地址
    $('.address-tbl').delegate('.zdelete','click',function () {
        if($(this).parents('tr').children('.col-note').children().hasClass('default')){
            $(this).parents('tr').next().children('.col-note').children().removeClass('zdefault').addClass('default');
        }
        $(this).parents('tr').remove();
    })

    function saveAddress(isPopAddress){

        var $data = $('#address_info').serialize();
        var url = $('#address_info').attr('action');
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: $data,
            success:function (data) {
                if (data.code == 0) {
                    layer.alert('保存成功', {
                        icon: 1
                    }, function () {
                        if(isPopAddress){
                            window.location.reload()
                        }
                        else{
                            window.location.href = '/member/address';
                        }
                    });
                }else{
                    // $.MsgBox.Alert('温馨提示','取消收藏失败');
                    layer.alert(data.message, {
                        icon: 2
                    });
                }
            },
            error:function(err){
                console.log(err)
            }
        })
    }
})